{% extends "base.html" %}

{% block title %}在线用户管理 - 协同调度信息系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <div class="d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">
                        <i class="fas fa-users"></i> 在线用户管理
                    </h5>
                    <div>
                        <a href="{{ url_for('decision_dashboard') }}" class="btn btn-secondary btn-sm">
                            <i class="fas fa-arrow-left"></i> 返回工作台
                        </a>
                        <button class="btn btn-primary btn-sm" onclick="location.reload()">
                            <i class="fas fa-sync-alt"></i> 刷新
                        </button>
                    </div>
                </div>
            </div>
            <div class="card-body">
                {% if active_sessions %}
                <div class="alert alert-info">
                    <i class="fas fa-info-circle"></i> 当前共有 <strong>{{ active_sessions|length }}</strong> 个活跃会话
                </div>
                
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>会话ID</th>
                                <th>用户名</th>
                                <th>邮箱</th>
                                <th>角色</th>
                                <th>登录时间</th>
                                <th>最后活动</th>
                                <th>IP地址</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for session, user in active_sessions %}
                            <tr>
                                <td><code>{{ session.id }}</code></td>
                                <td>
                                    <i class="fas fa-user"></i> {{ user.username }}
                                    {% if user.id == current_user.id %}
                                    <span class="badge bg-primary">当前用户</span>
                                    {% endif %}
                                </td>
                                <td>{{ user.email }}</td>
                                <td>
                                    <span class="role-badge {{ user.role }}-badge">
                                        {% if user.role == 'recon' %}侦察员
                                        {% elif user.role == 'analyst' %}分析员
                                        {% elif user.role == 'decision' %}决策员
                                        {% elif user.role == 'executor' %}执行员
                                        {% else %}{{ user.role }}
                                        {% endif %}
                                    </span>
                                </td>
                                <td>
                                    <small>{{ session.login_time.strftime('%Y-%m-%d %H:%M:%S') if session.login_time else 'N/A' }}</small>
                                </td>
                                <td>
                                    <small>{{ session.last_activity.strftime('%Y-%m-%d %H:%M:%S') if session.last_activity else 'N/A' }}</small>
                                </td>
                                <td>
                                    <small><i class="fas fa-network-wired"></i> {{ session.ip_address or 'N/A' }}</small>
                                </td>
                                <td>
                                    {% if session.is_active %}
                                    <span class="badge bg-success">
                                        <i class="fas fa-circle"></i> 在线
                                    </span>
                                    {% else %}
                                    <span class="badge bg-secondary">
                                        <i class="fas fa-circle"></i> 离线
                                    </span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if user.id != current_user.id %}
                                    <button class="btn btn-danger btn-sm" 
                                            onclick="kickUser({{ user.id }}, '{{ user.username }}')"
                                            title="踢出用户">
                                        <i class="fas fa-sign-out-alt"></i> 踢出
                                    </button>
                                    {% else %}
                                    <button class="btn btn-secondary btn-sm" disabled title="不能踢出自己">
                                        <i class="fas fa-ban"></i> 自己
                                    </button>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% else %}
                <div class="alert alert-warning">
                    <i class="fas fa-exclamation-triangle"></i> 当前没有活跃的用户会话
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- 踢出用户确认模态框 -->
<div class="modal fade" id="kickUserModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title">
                    <i class="fas fa-exclamation-triangle"></i> 确认踢出用户
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>您确定要踢出用户 <strong id="kickUsername"></strong> 吗？</p>
                <p class="text-muted">该用户的所有活跃会话将被终止，需要重新登录。</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="kickUserForm" method="POST" style="display: inline;">
                    <button type="submit" class="btn btn-danger">
                        <i class="fas fa-sign-out-alt"></i> 确认踢出
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

{% endblock %}

{% block extra_js %}
<script>
function kickUser(userId, username) {
    // 设置模态框内容
    document.getElementById('kickUsername').textContent = username;
    document.getElementById('kickUserForm').action = '/admin/kick_user/' + userId;
    
    // 显示模态框
    var modal = new bootstrap.Modal(document.getElementById('kickUserModal'));
    modal.show();
}

// 自动刷新（可选）
// setInterval(function() {
//     location.reload();
// }, 30000); // 每30秒刷新一次
</script>
{% endblock %}

